/**
 * 
 */
package com.gr.staffpm.widget.dialog.upload;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.lang.Bytes;

import com.gr.staffpm.properties.PropertiesController;

/**
 * @author Graham Rhodes 17 Apr 2011 19:26:33
 */
public abstract class FileUploadPanel extends Panel {

    private static final long serialVersionUID = 1L;

    private class FileUploadForm extends Form<Void> {

        private static final long serialVersionUID = 1L;
        private FileUploadField fileUploadField;

        /**
         * Construct.
         * 
         * @param name
         *            Component name
         */
        public FileUploadForm(String name) {
            super(name);

            // set this form to multipart mode (allways needed for uploads!)
            setMultiPart(true);

            // Add one file input field
            add(fileUploadField = new FileUploadField("fileInput"));

            // Set maximum size to 5120K (5MB)
            setMaxSize(Bytes.kilobytes(PropertiesController.getInstance().getPropertyAsInt("file.upload.max", 5120)));
        }

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @Override
        protected void onSubmit() {
            FileUploadPanel.this.onSubmit(fileUploadField.getFileUpload());
        }
    }

    public FileUploadPanel(String id) {
        super(id);

        // Create feedback panels
        final FeedbackPanel uploadFeedback = new FeedbackPanel("uploadFeedback");

        // Add uploadFeedback to the page itself
        add(uploadFeedback);

        // Add upload form with ajax progress bar
        final FileUploadForm ajaxSimpleUploadForm = new FileUploadForm("ajax-simpleUpload");

        add(ajaxSimpleUploadForm);

    }

    public abstract void onSubmit(FileUpload fileUpload);

}
